/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/

#include "pch.h"
#include <db/db_field_definition_t.h>
#include <db/db_tuple_values.h>

namespace test_wrappers { namespace storage {

tuple_values::tuple_values(
    const std::shared_ptr<tuple_instance>& tupleInstance,
    const std::shared_ptr<const std::vector<std::shared_ptr<field_value>>>& fieldValues
    ) :
    _tupleInstance(tupleInstance),
    _fieldValues(fieldValues)
{
}

std::shared_ptr<field_value> tuple_values::findValue(
    const field_id_b& userFieldId
    )
{
    // find the instance id...
    bool found = false;
    field_id instanceId = 0;

    const std::vector<std::shared_ptr<field_instance>>& fieldInstances = _tupleInstance->fieldInstances();
    for (auto &fieldInstance : fieldInstances)
    {
        if (fieldInstance->userFieldId() == userFieldId.id)
        {
            instanceId = fieldInstance->fieldInstanceId();
            found = true;
            break;
        }
    }

    if (!found)
    {
        throw db_exception("field definition id not found", e_db_field_definition_id_not_found);
    }

    for (auto &value : *_fieldValues)
    {
        if (value->fieldInstanceId() == instanceId)
        {
            return value;
        }
    }
    _ASSERTE(false);
    return nullptr;
}

std::shared_ptr<field_value_t<unsigned short>> tuple_values::field(
    const field_id_t<unsigned short>& userFieldId
    )
{
    std::shared_ptr<field_value_t<unsigned short>> fieldInstance = 
        std::dynamic_pointer_cast<field_value_t<unsigned short>>(findValue(userFieldId));

    return fieldInstance;
}

std::shared_ptr<field_value_t<unsigned int>> tuple_values::field(
    const field_id_t<unsigned int>& userFieldId
    )
{
    std::shared_ptr<field_value_t<unsigned int>> fieldInstance = 
        std::dynamic_pointer_cast<field_value_t<unsigned int>>(findValue(userFieldId));

    return fieldInstance;
}

std::shared_ptr<field_value_t<unsigned __int64>> tuple_values::field(
    const field_id_t<unsigned __int64>& userFieldId
    )
{
    std::shared_ptr<field_value_t<unsigned __int64>> fieldInstance = 
        std::dynamic_pointer_cast<field_value_t<unsigned __int64>>(findValue(userFieldId));

    return fieldInstance;
}

std::shared_ptr<field_value_t<std::wstring>> tuple_values::field(
    const field_id_t<std::wstring>& userFieldId
    )
{
    std::shared_ptr<field_value_t<std::wstring>> fieldInstance = 
        std::dynamic_pointer_cast<field_value_t<std::wstring>>(findValue(userFieldId));

    return fieldInstance;
}

std::shared_ptr<field_value_t<binary_blob>> tuple_values::field(
    const field_id_t<binary_blob>& userFieldId
    )
{
    std::shared_ptr<field_value_t<binary_blob>> fieldInstance = 
        std::dynamic_pointer_cast<field_value_t<binary_blob>>(findValue(userFieldId));

    return fieldInstance;
}

std::shared_ptr<const std::vector<std::shared_ptr<field_value>>> tuple_values::getValues() const
{
    return _fieldValues;
}

std::shared_ptr<tuple_instance> tuple_values::tupleInstance() const
{
    return _tupleInstance;
}

}} // namespace test_wrappers::storage
